package com.example.children.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.children.DTO.AttendanceTeacherDTO;
import com.example.children.DTO.StudentClassDTO;
import com.example.children.Exception.AttendanceException;
import com.example.children.Service.AttendanceService;
import com.example.children.Util.Tools;
import com.example.children.Util.UUIDGenerator;
import com.example.children.Util.tool;
import com.example.children.Model.Attendance;
import com.example.children.Model.ResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/Attendance")
public class AttendanceController {

    @Autowired
    private AttendanceService attendanceService;


    // 分页所有出勤记录
    @GetMapping("/All")
    public ResponseModel<Page<AttendanceTeacherDTO>> getAttendanceByStudent(
            @RequestParam(defaultValue = "1") int pageNo,  // 默认页码为1
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        System.out.println("进入查询出勤All方法：");
        Integer code= Tools.Success;
        String msg= tool.success;
        Page<AttendanceTeacherDTO> attendanceTeacherDTO=attendanceService.GetAllAttendance(pageNo,pageSize);
        if(attendanceTeacherDTO == null || attendanceTeacherDTO.getRecords().isEmpty()){
            System.out.println("没有出勤数据");
            msg= tool.fail;
        }
        return new ResponseModel<>(attendanceTeacherDTO,code,msg,null);//响应给前端的对象
    }

    // 通过学生ID查询出勤记录
    @GetMapping("/getAttendanceByStudent")
    public ResponseModel<List<Attendance>> getAttendanceByStudent(@RequestParam String studentId) {
        System.out.println("进入查询出勤getAttendanceByStudent方法：");
        Integer code= Tools.Success;
        String msg= tool.success;
        List<Attendance> attendances=attendanceService.listToStudentIds(studentId);
        if(attendances == null || attendances.isEmpty()){
            System.out.println("没有出勤数据");
            msg= tool.fail;
        }
        return new ResponseModel<>(attendances,code,msg,null);//响应给前端的对象
    }

    // 添加出勤记录
    @PostMapping("/addAttendance")
    public ResponseModel<Attendance> addAttendance(@RequestBody Attendance attendance) {
        System.out.println("进入addAttendance方法：");
        Integer code= Tools.Success;
        String msg= tool.success;
        String uuid = UUIDGenerator.generateNumericUUID(8);
        attendance.setAttendanceId(uuid);
        try {
            // 执行保存操作
            if (attendanceService.addAttendance(attendance)) {
                return new ResponseModel<>(attendance, code, msg, null); // 成功响应
            } else {
                throw new AttendanceException("保存失败！！！,student_id不存在");
            }
        } catch (DataAccessException e) {
            // 捕获 Spring 的数据库异常
            throw new AttendanceException("数据库操作失败，请稍后再试。"+e, e);
        } catch (Exception e) {
            // 捕获其他类型的异常
            throw new AttendanceException("发生未知错误，请稍后再试。"+e, e);
        }
    }

    // 修改出勤记录
    @PostMapping("/updateAttendance")
    public ResponseModel<Attendance> updateAttendance(@RequestBody Attendance attendance) {
        System.out.println("进入updateAttendance方法：");
        Integer code= Tools.Success;
        String msg= tool.success;
        if(!attendanceService.updateById(attendance))
            msg=tool.fail;
        return new ResponseModel<>(attendance, code, msg, null); // 成功响应
    }

    // 删除出勤记录
    @DeleteMapping("/deleteAttendance")
    public ResponseModel<String> deleteAttendance(@RequestParam String attendanceId) {
        System.out.println("进入deleteAttendance方法：");
        Integer code= Tools.Success;
        String msg= tool.success;
        if(!attendanceService.removeById(attendanceId))
            msg=tool.fail;
        return new ResponseModel<>(attendanceId, code, msg, null); // 成功响应
    }

    // 日期搜索出勤记录
    @PostMapping("/selectByDate")
    public ResponseModel<List<Attendance>> selectByDateSend(@RequestBody Attendance attendance) {
        System.out.println("进入selectByDate方法："+attendance.getDate()+"  "+attendance.getStudentId());
        Integer code= Tools.Success;
        String msg= tool.success;
        List<Attendance> attendances=attendanceService.selectByDateSend(attendance);
        if(attendances == null || attendances.isEmpty())
            msg=tool.fail;
        return new ResponseModel<>(attendances, code, msg, null); // 成功响应
    }

    // 搜索记录
    @PostMapping("/AdminSelectByDate")
    public ResponseModel<List<AttendanceTeacherDTO>> AdminselectByDate(@RequestBody AttendanceTeacherDTO attendance) {
        System.out.println("进入AdminSelectByDate方法："+attendance.getDate()+"  "+attendance.getStudentId());
        Integer code= Tools.Success;
        String msg= tool.success;
        List<AttendanceTeacherDTO> attendances=attendanceService.AdminselectByDateSend(attendance);
        if(attendances == null || attendances.isEmpty())
            msg=tool.fail;
        return new ResponseModel<>(attendances, code, msg, null); // 成功响应
    }





}

